<?php


namespace app\logic\model;


use think\Model;
class MsgLogModel extends Model
{
    protected $name='msg_log';
    protected $opus_number='{opus_number}';
    protected $opus_title='{opus_title}';
    protected $nickname='{nickname}';
    function send($templateId,$user_id,$data){
        $msg_template=(new MsgTemplateModel())->getFindById($templateId);
        if(!empty($msg_template)){
            $content=$this->strPlace($msg_template['content'],$data);
            $create_data=[
                'user_id'=>$user_id,
                'content'=>$content,
                'template_id'=>$templateId,
                'create_time'=>date('Y-m-d H:i:s'),
                'update_time'=>date('Y-m-d H:i:s'),
            ];
            if($this->save($create_data)){
                return true;
            }
            return false;
        }
        return false;
    }
    private function strPlace($content,$data){
        $content = str_replace($this->opus_number,$data['opus_number'],$content);//作品编码
        $content = str_replace($this->nickname,$data['nickname'],$content);//用户昵称
        $content = str_replace($this->opus_title,$data['opus_title'],$content);//作品标题
        return $content;
    }
    
         function sendSiteData($templateId,$user_id,$data){
        $msg_template=(new MsgTemplateModel())->getFindById($templateId);
        if(!empty($msg_template)){
            $content=$this->strPlaceData($msg_template['content'],$data);
            $create_data=[
                'user_id'=>$user_id,
                'content'=>$content,
                'template_id'=>$templateId,
                'create_time'=>date('Y-m-d H:i:s'),
                'update_time'=>date('Y-m-d H:i:s'),
            ];
            if($this->save($create_data)){
                return true;
            }
            return false;
        }
        return false;
    }
    
    private function strPlaceData($content,$data){
    	
    	foreach($data as $k=>$v)
    	{
    		 $content = str_replace('{'.$k.'}',$v,$content);
    	}
    	return $content;
    }
}